import changeTime from '@/components/changeTime.vue'
import formalitles from '@/components/carDetail/formalities.vue'
import carDetail from '@/components/carDetail/carDetail.vue'
export default {
  data() {
    return {
      agree: true,
      show: false,
      mode: 'center',
      maiState: null, //当前用户是卖方还是买方 1 我是买家  2 我是卖家

      dealMsg: {}, //交易信息
      proTimeshow: false, //生产日期状态
      certificateshow: false, //车辆合格证状态
      cardetailshow: false, //
      dealCarshow: false, //交车日期
      certificateList: [{
        label: '随车',
        value: 0
      }, {
        label: '邮寄',
        value: 1
      }],
      cardetailList: [{
        label: '现车现票/不店保',
        value: 0
      }, {
        label: '现车现票/店保',
        value: 1
      }, {
        label: '质损车源',
        value: 2
      }, {
        label: '期货/店保',
        value: 3
      }, {
        label: '其他',
        value: 4
      }],
      sellingPrice: '', //车辆销售价
      depositAmount: '', //定金金额
      deliverydate: {}, //交车日期
      proTime: '',//选择的生产日期
      certificate: null,//选择的车辆合格证
      cardetails: {},//选择的车辆详情
      remarks: '',

      ransfertCar: false, //交车日期弹框
      carDateList: [{
        value: 0,
        label: '7日内交车'
      }, {
        value: 1,
        label: '15日内交车'
      }, {
        value: 2,
        label: '30日内交车'
      }],

      bframe: false,
    }
  },
  components: {
    changeTime,
    formalitles,
    carDetail
  },
  onLoad(option) {
    this.dealMsg = option.data ? JSON.parse(decodeURIComponent(option.data)) : {}

    if (JSON.stringify(this.dealMsg) !== '{}') {
      this.maiState = this.dealMsg.maiState
      this.dealMsg.color = JSON.parse(this.dealMsg.color)
      if(this.dealMsg.goInPage==='buycar'){
        this.certificateList.forEach(it => {
          if (it.value == this.dealMsg.qualified) {
            this.dealMsg.qualifiedName = it.label
          }
        })
        this.cardetailList.forEach(it => {
          if (it.value == this.dealMsg.cardetails) {
            this.dealMsg.cardetailsName = it.label
          }
        })
        this.dealMsg.produceddate=this.dealMsg.produceddate.split(' ')[0]
      }else{
        this.dealMsg.carMsg=JSON.parse(this.dealMsg.carMsg)
        if(this.dealMsg.pageType==='findcar'){
        	this.proTime=this.dealMsg.quotationMsg.produced_Date.split('T')[0],//生产日期
        	this.certificate=this.dealMsg.quotationMsg.service_ChargeName,//合格证
        	this.cardetails.value=this.dealMsg.quotationMsg.car_Details,//车辆详情
          this.cardetails.label=this.dealMsg.quotationMsg.car_DetailsName,//车辆详情
        	this.depositAmount=this.dealMsg.quotationMsg.deposit//定金金额

        }
      }
    }
    // 1 我是买家  2 我是卖家
    console.log('担保交易参数',this.maiState)
    this.contractid = option.contractid
  },

  methods: {
    addressSeller(){
      let add = this.dealMsg.goInPage === 'index' ? this.dealMsg.seller.address : this.dealMsg.selleraddress
      return add
    },
    addressBuyers(){
      let add = this.dealMsg.goInPage === 'index' ? this.dealMsg.buyers.address : this.dealMsg.buyeraddress
      return add
    },

    selectRansferCarDate(val) {
      // console.log('交车日期', val)
      this.ransfertCar = false
      this.deliverydate= val
    },

    // 确认发起合同
    initiatingContract() {
      let data = {}
      let status=false
      let type=0
      uni.showLoading({
        title: '加载中'
      })
      if(this.goInPage==='index'){
			  status=this.proTime && this.certificate && this.cardetails && this.sellingPrice && this.depositAmount && this.deliverydate
        if(!this.sellingPrice){
					 type=1
				 }
				 if(!this.depositAmount){
					 type=2
				 }
      }else{
			  status=this.sellingPrice && this.depositAmount  && this.deliverydate
        if(!this.sellingPrice){
					 type=1
				 }
				 if(!this.depositAmount){
					 type=2
				 }
      }
			 if(status){
				 // 发起担保交易 需要填写车辆信息
        if(this.dealMsg.goInPage==='index'){
          data = {
				  'selleruserid': this.dealMsg.seller.id,//卖方用户id
				  'sellerstoreid': this.dealMsg.sellstoreid,//this.dealMsg.sellstoreid,//卖方店铺id
				  'buyerstoreid': this.dealMsg.buystoreid,//this.dealMsg.buystoreid,//买方店铺id
            'buyeruserid': this.dealMsg.buyeruserid,
				  'carid': this.dealMsg.pageType==='findcar'?this.dealMsg.quotationMsg.findcarId:this.dealMsg.carMsg.configulist.id,//车辆id
				  'color': JSON.stringify(this.dealMsg.color),//车辆颜色
				  'guideprice': this.dealMsg.carMsg.configulist.factoryprice,//指导价
				  'produceddate': this.proTime,//生产日期
				  'qualified': this.certificate,//合格证
				  'cardetails': this.cardetails.value,//车辆详情
				  'sales_price': this.sellingPrice,//销售金额
				  'deposit_amount': this.depositAmount,//定金金额
				  'delivery_date': this.deliverydate.value,//交车日期
				  'remarks': this.remarks,//备注
            'contracttype': 1,//合同类型（0、车源合同 1、发起担保交易）
            'carname': this.dealMsg.carMsg.series.carTypeName+this.dealMsg.carMsg.configulist.name
          }
        }else{
          data = {
				  'selleruserid': this.dealMsg.selleruserid,//卖方用户id
				  'sellerstoreid': this.dealMsg.sellerstoreid,//卖方店铺id
				  'buyerstoreid': this.dealMsg.buyerstoreid,//买方店铺id
            'buyeruserid': this.dealMsg.buyeruserid,
				  'carid': this.dealMsg.carid,//车辆id
				  'color': JSON.stringify(this.dealMsg.color),//车辆颜色
				  'guideprice': this.dealMsg.guideprice,//指导价
				  'produceddate': this.dealMsg.produceddate,//生产日期
				  'qualified': this.dealMsg.qualifiedName,//合格证
				  'cardetails': this.dealMsg.cardetails,//车辆详情
				  'sales_price': this.sellingPrice,//销售金额
				  'deposit_amount': this.depositAmount,//定金金额
				  'delivery_date': this.deliverydate.value,//交车日期
				  'remarks': this.remarks,//备注
            'contracttype': 0,
            'carname': this.dealMsg.carname
          }
        }
        let promise=''
        if(this.dealMsg.pageType==='findcar'){
          promise=this.$api.carOrder.SubmitContractTaskFindcar(data)
          data.contracttype=2
        }else{
          promise=this.$api.carOrder.SubmitContractTask(data)
        }
        promise.then(res => {
          console.log(res)
          if(res.code===200){
            console.log(res.data)
            this.show=false
            let curDate = new Date()
            let time= new Date(curDate.getTime() + 24*60*60*1000)
            let endtime=this.returnTime(time)
            console.log(endtime)
						 uni.hideLoading()
            // res.data.list[0].maiState=this.maiState
            // if(this.dealMsg.maiState===2){
            uni.navigateTo({
							     url: '/pages_order/orderAdmin/orderAdmin'
            })
            // }else{
            //   uni.navigateTo({
							     // url: '/pages_order/payDeposit/payDeposit?deposit_amount='+this.depositAmount+'&id='+res.data+'&time='+endtime
            //   })
            // }

          }
        })
      }else{
        uni.hideLoading()
        this.show=false
        let title=''
        if(type===1){
          title='请输入车辆销售价'
        }else if(type ===2){
          title='请输入车辆定金金额且不能低于800元'
        }else{
					 title= '请完善交易车辆信息'
        }
        uni.showToast({
          title: title,
          duration: 2000,
          icon: 'none'
        })
      }
      // <navigator url="/pages_order/payDeposit/payDeposit">
    },
    // 返回y-m-d h-m-m格式
    returnTime(d){

      let y=d.getFullYear()
      let m=(d.getMonth() + 1)<10?'0'+(d.getMonth() + 1):(d.getMonth() + 1)
      let day=d.getDate()<10?'0'+d.getDate():d.getDate()
      let h=d.getHours()<10?'0'+d.getHours():d.getHours()
      let mi=d.getMinutes()<10?'0'+d.getMinutes():d.getMinutes()
      let s=d.getSeconds()<10?'0'+d.getSeconds():d.getSeconds()
      s=s-1

      var datetime=y + '/' + m + '/' + day + ' ' + h + ':' + mi + ':' + s
      return datetime
    },
    // 选择生产日期
    selectProTime(obj) {
      // console.log(obj)

      var myDate = new Date()
      this.proTime = obj.year + '-' + obj.month + '-' + obj.day

      // 当前时间
      if (new Date(this.proTime) < myDate) {} else {
			  uni.showToast({
			    title: '生产日期不能大于当前时间',
			    duration: 2000,
			    icon: 'none'
			  })
			  this.proTime = ''
			  return
      }
    },
    // 选择合格证
    selectCertificate(obj) {
      this.certificate = obj.label
      this.certificateshow=false
    },
    //选择车辆详情
    selectCardetail(obj) {
      this.cardetails = obj
      this.cardetailshow=false

    },

    // 生产日期
    selectdealCar(obj) {
      this.dealMsg.produceddate = obj.year + '-' + obj.month + '-' + obj.day
    },
    getSellingPrice(val){
      console.log(val)
      this.sellingPrice=val.detail.value
    },
    getDepositAmount(val){
      console.log(val)
      this.depositAmount=val.detail.value>=800 ?val.detail.value:null
    },
    // 是否同意平台交易协议
    checkboxChange(val) {
      console.log(val)
      this.agree=val.value
    },
    // 提示阅读合同
    readDealFile(){
      uni.showToast({
			  title: '请阅读并同意相关交易协议',
			  duration: 2000,
			  icon: 'none'
      })
    },

    filtercolor(data) {
      data = JSON.parse(data)
      var arr = data.inimg.color.split('/')
      data.inimg.color = arr
      return data
    },

    xiaoxi() {
      uni.showToast({
        title: '该功能还未开发',
        icon: 'none',
      })
    },
    gonext(url) {
      uni.navigateTo({
        url: url
      })
    },
    textareablue(e){
      console.log(e.detail.value)
    }
  }
}
